package com.sitech.portal.system.service.impl;

import com.sitech.ibnms.common.form.TreeNode;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.portal.system.dao.ISysModuleDao;
import com.sitech.portal.system.domain.TblSYS_MODULE;
import com.sitech.portal.system.service.ISysModuleService;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SysModuleServiceImpl implements ISysModuleService {

	private ISysModuleDao sysModuleDao;	
	
	public ISysModuleDao getSysModuleDao() {
		return sysModuleDao;
	}

	public void setSysModuleDao(ISysModuleDao sysModuleDao) {
		this.sysModuleDao = sysModuleDao;
	}

	@Override
	public void add(TblSYS_MODULE tblSYS_MODULE) {
		String directory = sysModuleDao.getModuleDirectory(tblSYS_MODULE.PARENT_ID);
		if(directory!=null)
			directory += tblSYS_MODULE.MODULE_NAME + "/";
		else
			directory = tblSYS_MODULE.MODULE_NAME + "/";
		tblSYS_MODULE.setMODULE_DIRECTORY(directory);
		sysModuleDao.add(tblSYS_MODULE);
	}

	@Override
	public List checkPAGE_ELEMENT(String PAGE_ELEMENT) {
		// TODO Auto-generated method stub
		return sysModuleDao.checkPAGE_ELEMENT(PAGE_ELEMENT);
	}

	@Override
	public void deleteByPKs(String moduleId) {
		// TODO Auto-generated method stub
		String[] array = moduleId.split(",");
		for(int i=0;i<array.length;i++){
			String id = array[i];
			if(!id.equals(""))
				sysModuleDao.deleteByPKs(id);
		}
	}

	@Override
	public void deleteByRoleId(String roleId) {
		// TODO Auto-generated method stub
		sysModuleDao.deleteByRoleId(roleId);
	}

	@Override
	public List findFirstLevelModuleListByLoginID(String LOGIN_ID) {
		// TODO Auto-generated method stub
		return sysModuleDao.findFirstLevelModuleListByLoginID(LOGIN_ID);
	}

	@Override
	public List findFirstLevelModuleListByLoginIDAndSystemId(String LOGIN_ID,
			String SYSTEM_ID) {
		// TODO Auto-generated method stub
		return sysModuleDao.findFirstLevelModuleListByLoginIDAndSystemId(LOGIN_ID, SYSTEM_ID);
	}

	@Override
	public List findList() {
		// TODO Auto-generated method stub
		return sysModuleDao.findList();
	}

	@Override
	public IPage findListByPK(HashMap map, PageForm pageForm) {
		// TODO Auto-generated method stub
		return sysModuleDao.findListByPK(map, pageForm);
	}
	
	@Override
	public IPage findListByPKLoginId(HashMap map, PageForm pageForm){
		return sysModuleDao.findListByPKLoginId(map, pageForm);
	}

	@Override
	/**
	 * 获取菜单（带权限）
	 */
	public List<HashMap> findModuleListByLoginIDAndParentId(String LOGIN_ID,
			String parent_id) {
		// TODO Auto-generated method stub
		return sysModuleDao.findModuleListByLoginIDAndParentId(LOGIN_ID, parent_id);
	}

	@Override
	/**
	 * 获取菜单（不带权限）
	 */
	public List<HashMap> findModuleListByParentId(String parent_id) {
		// TODO Auto-generated method stub
		return sysModuleDao.findModuleListByParentId(parent_id);
	}

	@Override
	public TblSYS_MODULE findObjByPK(String id) {
		// TODO Auto-generated method stub
		return sysModuleDao.findObjByPK(id);
	}

	@Override
	public List findSecondLevelModuleListByLoginID(String MODULE_ID,
			String LOGIN_ID) {
		// TODO Auto-generated method stub
		return sysModuleDao.findSecondLevelModuleListByLoginID(MODULE_ID, LOGIN_ID);
	}

	@Override
	public List findlModuleListForTree(String ROLE_ID) {
		// TODO Auto-generated method stub
		return sysModuleDao.findlModuleListForTree(ROLE_ID);
	}

	@Override
	public List getAllModuleForReport(String loginId, String pre_super,
			int pre_super_len) {
		// TODO Auto-generated method stub
		return sysModuleDao.getAllModuleForReport(loginId, pre_super, pre_super_len);
	}

	@Override
	public int matchChar(String s, char ch) {
		// TODO Auto-generated method stub
		return sysModuleDao.matchChar(s, ch);
	}

	@Override
	public void update(TblSYS_MODULE tblSYS_MODULE) {
		// TODO Auto-generated method stub
		final String directory = StringUtils.defaultString(sysModuleDao.getModuleDirectory(tblSYS_MODULE.MODULE_ID));
		final String newDirectory = directory.replaceFirst("^(.*?)([^/]+)(/?)$", "$1" + tblSYS_MODULE.MODULE_NAME + "/");
		tblSYS_MODULE.setMODULE_DIRECTORY(newDirectory);
		sysModuleDao.update(tblSYS_MODULE);
		sysModuleDao.update(directory,newDirectory);
	}

	public List<TreeNode> buildModuleTreeForRoleConfig(String loginId,String systemId) {
		List<TreeNode> treeNods = new ArrayList<TreeNode>();
		
		List<HashMap> modules;
		if(loginId==null)
			modules = this.findModuleListByParentId(systemId);
		else
			modules = this.findModuleListByLoginIDAndParentId(loginId, systemId);
		
		if(modules!=null){
			for(HashMap map: modules){
				if(Integer.parseInt(map.get("MODULE_LEVEL").toString()) > 3){//由于平铺式菜单所限，不能多于三级
					continue;
				}
				TreeNode treeNode = new TreeNode();
				treeNode.setId(map.get("MODULE_ID").toString());
				treeNode.setpId(systemId);
				treeNode.setName(map.get("MODULE_NAME").toString());
				HashMap<String,String> userData = new HashMap<String,String>();
				userData.put("MODULE_PATH", ""+map.get("MODULE_PATH"));
				userData.put("IS_REFRESH", ""+map.get("IS_REFRESH"));
				userData.put("MODULE_LEVEL", ""+map.get("MODULE_LEVEL"));
				treeNode.setUserData(userData);
				treeNods.add(treeNode);
			}
		}
		return treeNods;
	}

	public List<TreeNode> buildModuleTreeForRoleConfig(String loginId,String systemId,List<TreeNode> treeNods) {
		List<HashMap> modules;
		if(loginId==null)
			modules = this.findModuleListByParentId(systemId);
		else
			modules = this.findModuleListByLoginIDAndParentId(loginId, systemId);
		if(modules!=null){
			for(HashMap map: modules){
				if(Integer.parseInt(map.get("MODULE_LEVEL").toString()) > 3){
					continue;
				}
				TreeNode treeNode = new TreeNode();
				treeNode.setId(map.get("MODULE_ID").toString());
				treeNode.setpId(systemId);
				treeNode.setName(map.get("MODULE_NAME").toString());
				HashMap<String,String> userData = new HashMap<String,String>();
				userData.put("MODULE_PATH", ""+map.get("MODULE_PATH"));
				userData.put("IS_REFRESH", ""+map.get("IS_REFRESH"));
				userData.put("MODULE_LEVEL", ""+map.get("MODULE_LEVEL"));
				userData.put("MODULE_TARGET", ""+map.get("MODULE_TARGET"));
				treeNode.setUserData(userData);
				if(map.get("CHILDREN")!=null && map.get("CHILDREN").toString().equals("0"))
					treeNode.setIsParent(false);
				treeNods.add(treeNode);
				
				buildModuleTreeForRoleConfig(loginId,map.get("MODULE_ID").toString(),treeNods);
			}
		}
		return treeNods;
	}

	@Override
	public List<TreeNode> loadAllNodeLevelByParentId(String loginId, String moduleId) {
		List<TreeNode> treeNods = new ArrayList<TreeNode>();
//		treeNods = buildModuleTreeForRoleConfig(loginId,moduleId,treeNods);
		
		List<HashMap> modules;
		if(loginId==null)
			modules = sysModuleDao.findAllModuleListByParentId(moduleId);
		else
			modules = sysModuleDao.findAllModuleListByLoginIDAndParentId(loginId, moduleId);
		
		if(modules!=null){
			for(HashMap map: modules){
				if(map.get("MODULE_LEVEL")==null || Integer.parseInt(map.get("MODULE_LEVEL").toString()) > 3){
					continue;
				}
				TreeNode treeNode = new TreeNode();
				treeNode.setId(map.get("MODULE_ID").toString());
				treeNode.setpId(map.get("PARENT_ID").toString());
				treeNode.setName(map.get("MODULE_NAME").toString());
				HashMap<String,String> userData = new HashMap<String,String>();
				userData.put("MODULE_PATH", ""+map.get("MODULE_PATH"));
				userData.put("IS_REFRESH", ""+map.get("IS_REFRESH"));
				userData.put("MODULE_LEVEL", ""+map.get("MODULE_LEVEL"));
				userData.put("MODULE_TARGET", ""+map.get("MODULE_TARGET"));
                userData.put("ENABLE", ""+map.get("MODULE_ENABLE"));
                treeNode.setUserData(userData);
				if(map.get("CHILDREN")!=null && map.get("CHILDREN").toString().equals("0"))
					treeNode.setIsParent(false);
				treeNods.add(treeNode);
				
			}
		}
		
		return treeNods;
	}
	public List<TreeNode> loadNextNodeLevelByParentId(String loginId, String moduleId) {
		List<TreeNode> treeNods = new ArrayList<TreeNode>();
		treeNods = buildModuleTreeForRoleConfig(loginId,moduleId);
		return treeNods;
	}

}
